/*
 * Syntelos 'SX'
 * Copyright (C) 1998, 2007  John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package syntelos.sx.methods;

import alto.lang.InputStream;
import alto.sys.File;
import alto.sys.Reference;

/**
 * Implements Conditional PUT using optional client supplied headers
 * <code>"If-None-Match"</code> and <code>"If-Modified-Since"</code>.
 * This permits batch puts to operate as a synchronization, and to not
 * create new versions unnecessarily.
 * 
 * @since 1.6
 */
public class Put
    extends syntelos.sx.Response
{

    public Put(){
        super();
    }


    public void respond(syntelos.sx.Request req)
        throws java.io.IOException,
               java.lang.InterruptedException
    {
        if (req.hasReference()){
            if (this.isAuthenticated()){
                Reference reference = req.getReference();
                boolean created = reference.existsNotStorage();
                if (!created){
                    /*
                     * Conditional PUT
                     */
                    String etag = req.getIfNoneMatch();
                    if (null != etag && etag.equals(reference.getETag())){
                        this.setETag(reference.getETag());
                        this.setLastModified(reference.getLastModified());
                        this.setStatusNoContent();
                    }
                    else {
                        long modif = reference.lastModified();
                        long since = req.getIfUnmodifiedSince();
                        if (0L < since && modif >= since){
                            this.setETag(reference.getETag());
                            this.setLastModified(modif);
                            this.setStatusNoContent();
                        }
                    }
                }
                /*
                 * Access control is under the write
                 */
                if (reference.put(req)){
                    this.setETag(reference.getETag());
                    this.setLastModified(reference.getLastModifiedString());
                    if (created)
                        this.setStatusCreated();
                    else
                        this.setStatusOk();
                }
                else {
                    this.setStatusBadRequest();
                }
            }
            else {
                this.setStatusUnauthorized();
            }
        }
        else {
            this.setStatusBadRequest();
        }
    }
}
